using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SD.LLBLGen.Pro.ORMSupportClasses;
using CARMSData.CollectionClasses;
using CARMSData.EntityClasses;
using CARMSData.FactoryClasses;
using CARMSData.HelperClasses;

namespace CARMS.Controls
{
    public partial class ConditionControl : UserControlBase
    {

        private ColumnToActionCollection CurrentColumnCollection
        {
            get
            {
                if (ViewState["CurrentColumnCollection"] == null)
                {
                    return new ColumnToActionCollection();
                }
                return (ColumnToActionCollection)ViewState["CurrentColumnCollection"];
            }

            set
            {
                ViewState["CurrentColumnCollection"] = value;
            }
        }

        private List<string> CurrentTextList
        {
            get
            {
                if (Session["CurrentTextList"] == null)
                {
                    return new List<string>();
                }
                return (List<string>)Session["CurrentTextList"];
            }

            set
            {
                Session["CurrentTextList"] = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

            }
        }

        public void InitalControls(string strActionID,string strTableName)
        {
            var collection = new ColumnToActionCollection();
            collection.GetMulti(ColumnToActionFields.ActionId == strActionID & ColumnToActionFields.TableName == strTableName, 0, new SortExpression { ColumnToActionFields.SortIndex | SortOperator.Ascending });
            CurrentColumnCollection = collection;
            listCondition.DataSource = collection;
            listCondition.DataBind(); 
        }


        public PredicateExpression GetPredicateExpression()
        {
            var condition = new PredicateExpression();
            foreach (ColumnToActionEntity entity in CurrentColumnCollection)
            {

                foreach (DataListItem item in listCondition.Items)
                {
                    var txtCondition = item.FindControl("txtCondition") as TextBox;
                    var dplCondition = item.FindControl("dplCondition") as DropDownList;

                    var txtColumnName = item.FindControl("txtColumnName") as TextBox;
                   

                    if (txtColumnName.Text.ToUpper() == entity.ColumnName.ToUpper())
                    { 
                        if (entity.ColumnType == ColumnType.ColumnInt.Value || entity.ColumnType == ColumnType.ColumnString.Value)
                        { 
                          
                            if (txtCondition.Text.Trim().Length > 0)
                            {
                                var field = Page.GetFieldByTableColumnName(entity.TableName, entity.ColumnName);
                                if (field != null)
                                {
                                    condition.Add((EntityField)field % ("%" + txtCondition.Text.Trim() + "%"));
                                }
                            }
                        }

                        if (entity.ColumnType == ColumnType.ColumnPublicItem.Value)
                        { 
                            if (dplCondition.SelectedValue.Trim().Length > 0)
                            {
                                var field = Page.GetFieldByTableColumnName(entity.TableName, entity.ColumnName);
                                if (field != null)
                                {
                                    condition.Add((EntityField)field ==new Guid(dplCondition.SelectedValue.Trim()));
                                }
                            }
                        }


          
                        if (entity.ColumnType == ColumnType.ColumnRelativeTable.Value)
                        {
                            if (txtCondition.Text.Trim().Length > 0)
                            {
                                var fieldMain = Page.GetFieldByTableColumnName(entity.TableName, entity.ColumnName);
                                if (fieldMain != null)
                                {
                                    var list = Page.GetColumnListByTableColumnName(entity.RelativeTable,
                                                                              entity.RelativeColumn,entity.RelativeSearchColumn,txtCondition.Text.Trim()); 

                                    var userFieldCompare = new FieldCompareRangePredicate((EntityField)fieldMain,
                                                                                          list.ToArray());
                                    condition.Add(userFieldCompare);
                                }
                            }
                        }
                    }
                }

            }

            return condition;
        }


        //public PredicateExpression GetPredicateExpression()
        //{
        //    var condition = new PredicateExpression();
        //    foreach (ColumnToActionEntity entity in CurrentColumnCollection)
        //    {

        //        foreach (var strTextID in CurrentTextList)
        //        {

        //            var txtObject = panelContent.FindControl(strTextID) as TextBox;

        //            if (txtObject.Text.Trim().Length > 0)
        //                    {
        //                        var field = GetFieldByTableColumnName(entity.TableName, entity.ColumnName);
        //                        if (field != null)
        //                        {
        //                            condition.Add((EntityField)field % ("%" + txtObject.Text.Trim() + "%"));
        //                        }
        //                    }



        //        }

        //    }

        //    return condition;
        //}

        protected void listCondition_OnItemDataBound(object sender, DataListItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                var entity = (ColumnToActionEntity)e.Item.DataItem;

                var txtCondition = e.Item.FindControl("txtCondition") as TextBox;
                var dplCondition = e.Item.FindControl("dplCondition") as DropDownList;

                txtCondition.Visible = true;
                dplCondition.Visible = false;

                if (entity.ColumnType == ColumnType.ColumnInt.Value)
                {
                     
                }

                if (entity.ColumnType == ColumnType.ColumnString.Value)
                {
                  
                }

                if (entity.ColumnType == ColumnType.ColumnPublicItem.Value)
                {
                    txtCondition.Visible = false;
                    dplCondition.Visible = true;
                    ConditionClass.BindPublicItemByCategoryID(dplCondition, entity.TypeValue);

                }

        

                if (entity.ColumnType == ColumnType.ColumnRelativeTable.Value)
                {
                    
                }
            }

        }

      
    }
}